package com.bgl.concurrency.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by BGL on 2018/3/20.
 */
@Slf4j
public class SyncTest2 {

    //修饰类
    public void test1(int j) {
        synchronized (SyncTest2.class) {
            for(int i = 0; i < 10; i++) {
                log.info("test1 - {} {}", j, i);
            }
        }
    }

    //修饰静态方法
    public static synchronized void test2(int j) {
        for(int i = 0; i < 10; i++) {
            log.info("test2 - {} {}", j, i);
        }
    }

    public static void main(String[] args) {
        SyncTest2 test1 = new SyncTest2();
        SyncTest2 test2 = new SyncTest2();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            test1.test1(1);
        });
        executorService.execute(() -> {
            test2.test1(2);
        });
    }
}
